package tcpandudp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class UrlDown {
    public static void main(String[] args) throws IOException {
//        URL url = new URL("http://v6-y.douyinvod.com/fb67dc5a3bf5ae418c0d3d87e8fb93c9/615039ef/video/tos/cn/tos-cn-ve-15/15acbcbb2ccc458ba39898e2126da63c/?a=1128&br=1613&bt=1613&btag=3&cd=0%7C0%7C0&ch=0&cr=0&cs=0&cv=1&dr=0&ds=3&er=&ft=OR_LrKZZI0VK1iczQTh94ictRsWd.Vv~R68&l=202109261614080102122040452A043137&lr=&mime_type=video_mp4&net=0&pl=0&qs=0&rc=amZrd2g6Zmg7ODMzNGkzM0ApNDg4ODVnNjs3N2Q2NDY1O2cpaGRqbGRoaGRmai9kcXI0Zy40YC0tZC0wc3MtLS0vNTEwYWIwXjQuY2M2OmNwb2wrbStqdDo%3D&vl=&vr=");
//        URL url = new URL("https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300fg10000c5781ejc77uettgh2cmg&ratio=720p&line=0");
        URL url = new URL("https://www.bilibili.com/video/BV1LL4y1E7MX?spm_id_from=333.851.b_7265636f6d6d656e64.4");
//        Object content = url.getContent();
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        // 不支持java程序请求，模拟手机浏览器
        urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//        String responseMessage = urlConnection.getResponseMessage();

        String location = urlConnection.getHeaderField("location");
        URL url1 = new URL(location);
        URLConnection urlConnection1 = url1.openConnection();
        urlConnection1.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        InputStream inputStream = urlConnection1.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream("4.mp4");
        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes,0,len);
        }
        fileOutputStream.close();
        inputStream.close();
        urlConnection.disconnect();
    }
}
